<template>
  <div>
    <div class="clear" id="caozuo">
      <el-form :inline="true" class="demo-form-inline">
        <el-select
          v-model="value"
          placeholder="请选择寄养状态"
          @change="onSelect"
        >
          <el-option label="寄养中" value="寄养中"></el-option>
          <el-option label="待确认" value="待确认"></el-option>
          <el-option label="已归还" value="已归还"></el-option>
        </el-select>

        <el-form-item>
          <el-button type="primary" @click="reset" class="button"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <el-table :data="dangqianyeData" style="width: 100%">
      <el-table-column label="序号" align="center" prop="id" width="180">
      </el-table-column>
      <el-table-column label="用户" align="center" prop="user">
      </el-table-column>
      <el-table-column label="宠物昵称" align="center" prop="petname">
      </el-table-column>
      <el-table-column label="寄养时间" align="center" prop="created">
      </el-table-column>
      <el-table-column label="寄养天数" align="center" prop="days">
      </el-table-column>
      <el-table-column label="寄养状态" align="center" prop="jystatus">
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <div class="clear" id="button">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </div>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      layout="prev, pager, next"
      @current-change="changePage"
      :current-page="dangqianyema"
      :page-count="maxPage"
      :total="datatotal"
      :page-size="10"
    >
    </el-pagination>
    <!-- 编辑弹窗 -->
    <el-dialog
      title="修正寄养信息"
      :visible.sync="editDialogVisible"
      width="30%"
    >
      <el-form ref="form" label-width="70px" :model="rowData">
        <el-form-item label="序号">
          <el-input v-model="rowData.id" disabled></el-input>
        </el-form-item>
        <el-form-item label="用户">
          <el-input v-model="rowData.user"></el-input> </el-form-item
        ><el-form-item label="宠物名">
          <el-input v-model="rowData.petname"></el-input> </el-form-item
        ><el-form-item label="寄养日期">
          <el-input v-model="rowData.jydate"></el-input> </el-form-item
        ><el-form-item label="寄养天数">
          <el-input v-model="rowData.days"></el-input>
        </el-form-item>
        <el-form-item label="寄养状态">
          <el-select v-model="rowData.jystatus" placeholder="请选择寄养状态">
            <el-option label="寄养中" value="寄养中"></el-option>
            <el-option label="待确认" value="待确认"></el-option>
            <el-option label="已归还" value="已归还"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="comfirmEdit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import axios from "axios";
export default {
  name: "Student",
  data() {
    return {
      formData: {
        jystatus: "",
      },
      value: "",
      rowData: {
        id: "",
        user: "",
        petname: "",
        jydate: "",
        days: "",
        jystatus: "",
      },

      stuList: [],
      dangqianyema: 1,
      maxPage: 0,
      editDialogVisible: false,
      datatotal: 0,
    };
  },
  computed: {
    dangqianyeData() {
      let qishi = (this.dangqianyema - 1) * 10;
      return this.stuList.slice(qishi, qishi + 10);
    },
  },
  created() {
    this.getdata();
  },
  beforeUpdate() {
    this.maxpage();
  },
  methods: {
    getdata() {
      axios
        .get("http://localhost:8081/jiyang/list")
        .then((response) => {
          this.stuList = response.data.data;
          this.datatotal = response.data.data.length;
        })
        .catch((error) => {
          console.error("There was an error!", error);
        });
    },
    reset() {
      this.value = "";
      this.getdata();
    },
    onSelect(value) {
      axios
        .get("http://localhost:8081/jiyang/getByStatus?status=" + this.value)
        .then((response) => {
          this.stuList = response.data.data;
          this.datatotal = response.data.data.length;
          // this.getdata();
        })
        .catch((error) => {
          console.error("There was an error!", error);
        });
    },
    handleEdit(index, row) {
      this.editDialogVisible = true;

      this.rowData.id = row.id;
      this.rowData.user = row.user;
      this.rowData.petname = row.petname;
      this.rowData.jydate = row.created;
      this.rowData.days = row.days;
      this.rowData.jystatus = row.jystatus;
    },
    comfirmEdit() {
      axios
        .post("http://localhost:8081/jiyang/updateByid", this.rowData)
        .then((response) => {
          this.stuList = response.data.data;
          this.datatotal = response.data.data.length;
          // this.getdata();
        })
        .catch((error) => {
          console.error("There was an error!", error);
        });
      this.editDialogVisible = false;
    },
    handleDelete(index, row) {
      axios
        .post("http://localhost:8081/jiyang/deleteByid?id=" + row.id)
        .then((response) => {
          this.stuList = response.data.data;
          this.datatotal = response.data.data.length;
        })
        .catch((error) => {
          console.error("There was an error!", error);
        });
    },
    changePage(page) {
      this.dangqianyema = page;
    },
    maxpage() {
      this.maxPage = Math.ceil(this.stuList.length / 10);
      console.log(this.maxPage);
    },
  },
};
</script>
<style scoped>
.clear::after {
  content: "";
  display: block;
  clear: both;
}

#caozuo .button {
  margin: 0 5px;
}
.el-table {
  margin: auto;
  margin-top: 20px;
}
.el-pagination {
  text-align: center;
  margin-top: 20px;
}
</style>
